﻿using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbCompInfoDal
	{
		public DbCompInfoDal()
		{
		}
		/// <summary>
		/// 读取时间空为"1900-01-01"
		/// </summary>
		/// <param name="dt">时间</param>
		/// <returns>返回时间</returns>
		private static DateTime ReadTime(DateTime dt)
		{
			DateTime refdt = Convert.ToDateTime("1900-01-01");
			if (!string.IsNullOrEmpty(dt.ToString()) && dt.Year != 1)
			{
				refdt = dt;
			}
			return refdt;
		}
		 		#region Insert
 		public static int Insert(DbCompInfo dbcompinfo_model)
		{	
			string sql = @"Insert Into dbCompInfo (				cCode,				cName,				cNotes,				cKeywords,				cDescription,				cStatus,				cAddTime,				cType,				cEndDate,				cStaDate
				) values('{0}','{1}','{2}','{3}','{4}',{5},'{6}',{7},'{8}','{9}')";
			sql = string.Format(sql,				SqlHelper.String(dbcompinfo_model.CCode),				SqlHelper.String(dbcompinfo_model.CName),				SqlHelper.String(dbcompinfo_model.CNotes),				SqlHelper.String(dbcompinfo_model.CKeywords),				SqlHelper.String(dbcompinfo_model.CDescription),				dbcompinfo_model.CStatus,				ReadTime(dbcompinfo_model.CAddTime),				dbcompinfo_model.CType,				ReadTime(dbcompinfo_model.CEndDate),				ReadTime(dbcompinfo_model.CStaDate)
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Insert_GetID
 		public static int Insert_GetID(DbCompInfo dbcompinfo_model)
		{	
			string sql = @"Insert Into dbCompInfo (				cCode,				cName,				cNotes,				cKeywords,				cDescription,				cStatus,				cAddTime,				cType,				cEndDate,				cStaDate
				) values('{0}','{1}','{2}','{3}','{4}',{5},'{6}',{7},'{8}','{9}')";
			sql = string.Format(sql,				SqlHelper.String(dbcompinfo_model.CCode),				SqlHelper.String(dbcompinfo_model.CName),				SqlHelper.String(dbcompinfo_model.CNotes),				SqlHelper.String(dbcompinfo_model.CKeywords),				SqlHelper.String(dbcompinfo_model.CDescription),				dbcompinfo_model.CStatus,				ReadTime(dbcompinfo_model.CAddTime),				dbcompinfo_model.CType,				ReadTime(dbcompinfo_model.CEndDate),				ReadTime(dbcompinfo_model.CStaDate)
				);
			int iReset = 0;
            SqlHelper.ExecSql(sql, ref iReset);
            return iReset;
		}
		#endregion
		
		#region Update
 		public static int Update(DbCompInfo dbcompinfo_model)
		{	
			string sql = @"Update dbCompInfo set 				cCode='{0}',				cName='{1}',				cNotes='{2}',				cKeywords='{3}',				cDescription='{4}',				cStatus={5},				cAddTime='{6}',				cType={7},				cEndDate='{8}',				cStaDate='{9}'				where				cId={10}";
			sql = string.Format(sql,				SqlHelper.String(dbcompinfo_model.CCode),				SqlHelper.String(dbcompinfo_model.CName),				SqlHelper.String(dbcompinfo_model.CNotes),				SqlHelper.String(dbcompinfo_model.CKeywords),				SqlHelper.String(dbcompinfo_model.CDescription),				dbcompinfo_model.CStatus,				ReadTime(dbcompinfo_model.CAddTime),				dbcompinfo_model.CType,				ReadTime(dbcompinfo_model.CEndDate),				ReadTime(dbcompinfo_model.CStaDate),				dbcompinfo_model.CId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 cId)
		{
		
			string sql = @"Delete from dbCompInfo where 
					cId= {0}";
			sql = string.Format(sql,
					cId);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from dbCompInfo a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbCompInfo Select(Int32 cId)
		{
			DbCompInfo dbcompinfo_model = null;
			string sql = @"select a.* from dbCompInfo a  where
					a.cId= {0}";
			sql = string.Format(sql,
					cId);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbcompinfo_model = new DbCompInfo(ds.Tables[0].Rows[0]);					
			return dbcompinfo_model;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbCompInfo a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbCompInfo a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from dbCompInfo a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}